package com.hydl.power.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.IdenticalMapper;
import com.hydl.power.pojo.ExtremeAnalyze;
import com.hydl.power.pojo.Identical;
import com.hydl.power.service.IdenticalService;
import com.hydl.power.utils.EasyPoiUtil;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author wxb
 * @date 2019/12/31 14:43
 */
@RestController
@RequestMapping("/identical")
@Api(value = "IdenticalController", tags = "环比分析")
@CrossOrigin
public class IdenticalController {
    @Autowired
    private IdenticalService identicalService;
    @Autowired
    private IdenticalMapper identicalMapper;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "YTD", value = "YTD", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "startTime", required = false, dataType = "String"),
    })

    // @LogAnnotation(module = "同比分析",description = "同比分析分页")
    @PostMapping(value = "/getAllIdenticalByPage")
    @ApiOperation(value = "环比分析分页", produces = "application/json")
    public Resp getAll(Integer pageNum, Integer pageSize, String YTD, String cityId, String projectId, String token, String startTime) {
        return Resp.success(identicalService.queryIdentical(pageNum, pageSize, YTD, cityId, projectId, token, startTime));
    }


    // @LogAnnotation(module = "同比分析",description = "同比分析导出")
    @GetMapping(value = "IdenticalExport")
    @ApiOperation(value = "环比分析导出")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "YTD", value = "YTD", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "startTime", required = false, dataType = "String"),
    })
    public void IdenticalExport(HttpServletRequest request, HttpServletResponse response, Integer pageNum, Integer pageSize, String YTD, String cityId, String projectId, String token, String startTime) throws IOException {
        String fileName = "";
        if (YTD.equals("day")) {
            fileName = "日环比分析";
        } else if (YTD.equals("month")) {
            fileName = "月环比分析";
        } else {
            fileName = "年环比分析";
        }
        PageInfo<Identical> list = identicalService.queryIdentical(pageNum, pageSize, YTD, cityId, projectId, token, startTime);
        List<Identical> arr = list.getList();
        EasyPoiUtil.exportExcel(EasyPoiUtil.getWorkbook(fileName, fileName, Identical.class, arr), fileName, response);
    }


}
